home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #14 / Monster Media No. 14 (April 1996) (Monster Media, Inc.).ISO / games_a / wordy410.zip / XTRACT.C < prev    next >
C/C++ Source or Header  |  1995-12-18  |  8KB  |  332 lines

  1. /**************************************************************************/
  2. /*                             Extract Utility                            */
  3. /*                                                                        */
  4. /*                                 M\Cooper                               */
  5. /*                          3425 Chestnut Ridge Rd.                       */
  6. /*                        Grantsville, MD 21536-9801                      */
  7. /*                        --------------------------                      */
  8. /*                        Email:  thegrendel@aol.com                      */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package              */
  11. /*                                                                        */
  12. /**************************************************************************/
  13.  
  14.  
  15. #include <conio.h>
  16. #include "srch.h"
  17.  
  18.  
  19. #define FILE_OPENING_ERROR 3
  20. #define FILENAME_MAXLEN 8
  21. #define CR "\n"
  22. #define FILE_SUFFIX ".xtr"
  23. #define MAXLEN 60
  24. #define LINE_LEN 80
  25. #define NOARGS 1
  26. #define INCREMENT 1
  27. #define SPACE ' '
  28. #define NOT "~!"
  29. #define SINGLE 1
  30. #define MINLEN 4
  31.  
  32. #define BUFFERSIZE 8192
  33. /*******8K buffer******/
  34.  
  35. char ad[] =
  36. "XTRACT tool by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  37.  
  38. typedef enum { FALSE, TRUE } Boolean;
  39.  
  40. void getword( char *lset, char *nlset, char *wrdfile );
  41. void center( char *strng );
  42. void parse( char *argument, char *lset, char *not_letterset );
  43. Boolean wordtest( char *letterset, char *word );
  44. char *nw_test( char *nlset, char *word );
  45.  
  46.  
  47. void main( int argc, char **argv )
  48. {
  49.  
  50.    char letterset[ MAXLEN ],
  51.        n_letterset[ MAXLEN ],
  52.        input_set[ MAXLEN ],
  53.     wfile [ MAXLEN ];  
  54.  
  55.      strcpy ( n_letterset, NULL );
  56.  
  57.      if( argc == NOARGS )
  58.         {
  59.         clrscr();
  60.         puts( "Enter a LETTERSET to test [~ or ! for excluded letters]..." );
  61.         gets( input_set );
  62.         parse( input_set, letterset, n_letterset );
  63.  
  64.       printf( "\n\n" );
  65.       puts( "Enter the name of the word file to search... " );
  66.       gets( wfile );
  67.  
  68.         }
  69.      else
  70.         parse( argv[1], letterset, n_letterset );
  71.  
  72.       if( argc == NOARGS + 1 )
  73.          strcpy( wfile, "word.lst" );
  74.       else
  75.          if( argc == NOARGS + 2 )
  76.             strcpy( wfile, argv [2] );
  77.  
  78.      getword( letterset, n_letterset, wfile );
  79. }
  80.  
  81.  
  82.  
  83. /**********************************WORDTEST********************************/
  84. /*       Function tests if word is constructible from Letterset           */
  85. /*                 Args in: char *letterset, char *word                   */
  86. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  87. /**************************************************************************/
  88.  
  89. Boolean wordtest( char *letterset, char *word )
  90. {
  91.    char temp [MAXLEN],
  92.        *t;
  93.    Boolean error_flag = TRUE;
  94.  
  95.      if( *letterset == NULL )
  96.         return( error_flag );  //All valid if no specs given
  97.  
  98.      strcpy( temp, word );  //Preserve WORD
  99.  
  100.      while( *letterset )
  101.         {
  102.         t = strchr( temp, *letterset++ );
  103.  
  104.         if( !t )
  105.           {
  106.           error_flag = FALSE;
  107.           return( error_flag );
  108.           } 
  109.         
  110.         *t = '*';  //Remove occurrence of letter
  111.         }
  112.  
  113.         return( error_flag );
  114. }
  115.  
  116. char *nw_test( char *nlset, char *word )
  117. {
  118.    char *ptr;
  119.  
  120.      ptr = ( strpbrk( word, nlset ) );
  121.  
  122.      return( ptr );
  123. }
  124.  
  125. /*************************************************************/
  126.  
  127. void getword( char *letter_set, char *nlset, char *wordfile )
  128. {
  129.  
  130.     char    l_set [ MAXLEN ],
  131.         word [ MAXLEN ],
  132.         tempstr [ LINE_LEN + 1 ],
  133.         targetfile [ MAXLEN ],
  134.         bar [ LINE_LEN + 1 ],
  135.         double_bar [ LINE_LEN + 1 ],
  136.   msg1 [ MAXLEN ], 
  137.   msg2 [ MAXLEN ];
  138.  
  139.     FILE *fptr,
  140.         *tfile;
  141.     int fnamelen;
  142.     long wcount = 0L;
  143.  
  144.        memset( bar, '-', LINE_LEN );
  145.        *( bar + LINE_LEN ) = NULL;
  146.        memset( double_bar, '=', LINE_LEN );
  147.        *( double_bar + LINE_LEN ) = NULL;
  148.  
  149.        /*************opening credits*************/
  150.        clrscr();
  151.        printf( double_bar );
  152.        strcpy( tempstr, ad );
  153.        center ( tempstr );
  154.        printf( tempstr );
  155.        printf( CR );
  156.        printf( double_bar );
  157.        printf( CR );
  158.        /****************************************/
  159.  
  160.  
  161.        strcpy ( l_set, letter_set );
  162. //       strcat ( letter_set, CR );
  163.  
  164.        /*   Create name of file to store derived words in   */
  165.        /*********************************************************/
  166.        fnamelen = strlen( l_set );
  167.  
  168.        if( fnamelen > 0 )
  169.          {
  170.          if( fnamelen  > FILENAME_MAXLEN )
  171.             fnamelen = FILENAME_MAXLEN;
  172.          strncpy( targetfile, l_set, fnamelen );
  173.          *( targetfile + fnamelen ) = NULL;
  174.             //NULL-terminate string, so strcat works, ha, ha.
  175.          }
  176.        else
  177.          strcpy( targetfile, "not" );
  178.  
  179.        strcat( targetfile, FILE_SUFFIX );
  180.        /*********************************************************/
  181.  
  182.        if( !( fptr = fopen( wordfile, "rt" ) ) )
  183.          {
  184.          printf( "\7\7\7Cannot open Wordfile!" );
  185.          exit( FILE_OPENING_ERROR );
  186.          }
  187.       if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
  188.          exit( FILE_OPENING_ERROR );
  189.  
  190.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  191.          {
  192.          printf( "\7\7\7Cannot open file to save words in!" );
  193.          exit ( FILE_OPENING_ERROR + 1 );
  194.          }
  195.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  196.          exit( FILE_OPENING_ERROR );
  197.  
  198.        /**************'Wait' Message************/
  199.        printf( CR CR );
  200.        printf( "WORKING...\n\n" );
  201.        printf( "This will take a few seconds.\n" );
  202.        printf( "Please be patient.\n\n" );
  203.        printf( "Now searching word file %s\nand writing file of valid words containing -%s-", 
  204.           wordfile, letter_set );
  205.  
  206.         if( *nlset )
  207.           printf( " but NOT -%s-", nlset );
  208.  
  209.         printf( ".\n\n" );
  210.        /*****************************************/
  211.  
  212.  
  213.  
  214.  
  215.  
  216.        sprintf( tempstr, "Word(s) containing: %s", strupr( l_set ) );
  217.        if( *nlset )
  218.             {
  219.             strcat( tempstr, ", but NOT -" );
  220.             strcat( tempstr, nlset );
  221.             strcat( tempstr, "-" );
  222.             }
  223.        strcat( tempstr, CR );
  224.  
  225.        center( tempstr );
  226.        fprintf( tfile, double_bar );
  227.     fprintf( tfile, CR );
  228.        fprintf( tfile, tempstr );
  229.        fprintf( tfile, double_bar );
  230.        fprintf( tfile, CR );
  231.  
  232.  
  233.          /*********************Main Loop*************/     
  234.           while( fgets( word, MAXLEN, fptr ) != NULL )
  235.  
  236.             if( wordtest( letter_set, word ) )
  237.                if( !nw_test( nlset, word ) )
  238.                  {
  239.                  fprintf( tfile, "%s", word );
  240.                  wcount++;
  241.                  }
  242.           /*******************************************/
  243.  
  244.       if( wcount == SINGLE )
  245.           {
  246.           strcpy( msg1, "word" );
  247.           strcpy( msg2, "word contains" );
  248.           }
  249.       else
  250.           {
  251.           strcpy( msg1, "words" );
  252.           strcpy( msg2, "words contain" );
  253.           }
  254.  
  255.           fprintf( tfile, bar );
  256.           sprintf( tempstr, "%ld %s %s.",
  257.                  wcount, msg2, l_set );
  258.           if( *nlset )
  259.             {
  260.             strcat( tempstr, ".. but NOT --- " );
  261.             strcat( tempstr, nlset );
  262.             strcat( tempstr, "." );
  263.             }
  264.  
  265.     fprintf( tfile, "\n" );
  266.           center( tempstr );              
  267.           fprintf( tfile, tempstr );
  268.           fprintf( tfile, "\n\n" );
  269.  
  270.           center( ad );
  271.           fprintf( tfile, ad );
  272.  
  273.           fcloseall();
  274.  
  275.           sprintf( tempstr,
  276.                  "The file %s has %ld %s containing %s.",
  277.                  targetfile, wcount, msg1, l_set );
  278.           if( *nlset )
  279.             {
  280.             strcat( tempstr, ".. but NOT --> " );
  281.             strcat( tempstr, nlset );
  282.             strcat( tempstr, "." );
  283.             }
  284.  
  285.           center( tempstr );
  286.           printf( CR CR );
  287.           printf( tempstr );
  288.           printf( "\7" );  //Bell
  289.  
  290. }
  291.  
  292.  
  293.  
  294. void center( char *str )
  295. {
  296.    int padding;
  297.    char st [ LINE_LEN + INCREMENT ];
  298.  
  299.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  300.      memset( st, SPACE, padding );
  301.      *( st + padding ) = NULL;  //Terminate string
  302.      strcat( st, str );
  303.      strcpy( str, st );
  304.  
  305.      return;
  306. }
  307.  
  308. void parse( char *arg, char *letterset, char *nls )
  309. {
  310.    char *p;
  311.  
  312.      if( *arg == '~' || *arg == '!' )
  313.         {
  314.         *letterset = NULL;
  315.         strcpy( nls, ++arg );
  316.         return;
  317.         }
  318.  
  319.  
  320.      p = strtok( arg, NOT );
  321.      strcpy( letterset, p );
  322.  
  323.      p = strtok( NULL, NULL );
  324.  
  325.      if( *p )
  326.         strcpy( nls, p );
  327.   else
  328.      *nls = NULL;
  329.  
  330.      return;
  331. }
  332.